/*
 * @ProjectName: 编程学习
 * @Copyright:   2019 HangZhou Yi Dev, Ltd. All Right Reserved.
 * @address:     https://yiyuery.github.io/
 * @date:        2019/5/20 20:57
 * @email:       xiazhaoyang@live.com
 * @description: 本内容仅限于编程技术学习使用，转发请注明出处.
 */
package com.example.swagger.transform;

import com.example.swagger.common.base.AbstractOfficeTransformPlugin;
import com.example.swagger.common.enums.FileTypeEnum;
import com.example.swagger.common.util.PDFHelper;
import com.example.swagger.common.util.TransformHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * PDF 转化为 IMAGE
 * </p>
 *
 * @author Yi
 * @version v1.0.0
 * @date 2019-06-09 10:06
 * @modificationHistory=========================逻辑或功能性重大变更记录
 * @modify By: {修改人} 2019-06-09
 * @modify reason: {方法名}:{原因}
 * ...
 */
@Slf4j
public class PdfAndImageTransformPlugin extends AbstractOfficeTransformPlugin {


    @Override
    public void gen() {
        gen(false);
    }

    /**
     * PDF转图片
     */
    @Override
    public void transform() {
        String source = TransformHelper.getTransformFilePath(FileTypeEnum.FILE_TYPE_PDF,FileTypeEnum.FILE_TYPE_IMAGE,FileTypeEnum.FILE_TYPE_PDF);
        String target = TransformHelper.getTransformFilePath(FileTypeEnum.FILE_TYPE_PDF,FileTypeEnum.FILE_TYPE_IMAGE,FileTypeEnum.FILE_TYPE_IMAGE);
        File pdf = Paths.get(source).toFile();
        try {
            TransformHelper.initFile(pdf);
            PDDocument doc = PDDocument.load(pdf);
            PDFRenderer renderer = new PDFRenderer(doc);
            int pageCount = doc.getNumberOfPages();
            for (int i = 0; i < pageCount; i++) {
                BufferedImage image = renderer.renderImageWithDPI(i, 240);
                String tempPath = target.replace(".png","-"+i+".png");
                File imageFile = Paths.get(tempPath).toFile();
                TransformHelper.initFile(imageFile);
                ImageIO.write(image, "PNG", imageFile);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 图片转PDF
     */
    @Override
    public void transformReversal() {
        String source = TransformHelper.getTransformFilePath(FileTypeEnum.FILE_TYPE_IMAGE,FileTypeEnum.FILE_TYPE_PDF,FileTypeEnum.FILE_TYPE_IMAGE);
        String target = TransformHelper.getTransformFilePath(FileTypeEnum.FILE_TYPE_IMAGE,FileTypeEnum.FILE_TYPE_PDF,FileTypeEnum.FILE_TYPE_PDF);
        File imageFolder = Paths.get(source).toFile();
        try {
            TransformHelper.initFile(imageFolder);
            List<String> imagePaths = new ArrayList<String>();
            Arrays.asList(Objects.requireNonNull(imageFolder.listFiles())).forEach(p -> {
                if (p.getName().endsWith(".png")) {
                    imagePaths.add(p.getAbsolutePath());
                }
            });
            imagePaths.sort(String::compareToIgnoreCase);
            //合成图片转pdf
            PDFHelper.createPDFFromImage(imagePaths, target);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
